package com.jshan.blog.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jshan.blog.base.admin.entity.SysRoleMenu;
import com.jshan.blog.service.SysMenuService;
import com.jshan.blog.service.SysRoleDictService;
import com.jshan.blog.service.SysRoleMenuService;
import com.jshan.blog.mapper.SysUserMenuMapper;
import com.jshan.blog.service.SysRoleService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author jshan
* @description 针对表【sys_user_menu(用户菜单对应表)】的数据库操作Service实现
* @createDate 2025-01-22 11:20:46
*/
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysUserMenuMapper, SysRoleMenu>
    implements SysRoleMenuService {

    @Resource
    private SysRoleDictService roleDictService;

    @Resource
    @Lazy
    private SysRoleService roleService;

    @Resource
    @Lazy
    private SysMenuService menuService;

    /**
     * 获取权限
     *
     * @param userTagCd
     * @return
     */
    @Override
    public Set<String> listPermAll(Integer userTagCd) {
        Set<String> permsSet = new HashSet<>();
        if (userTagCd != null) {
            Integer roleId = roleDictService.getRoleId(userTagCd);
            String rolePerm = roleService.getById(roleId).getPermissions();
            // String rolePerm = "";
            if (StringUtils.isBlank(rolePerm)){
                return permsSet;
            }
            if ("admin".equals(rolePerm)){
                permsSet = menuService.getPermAll();
            } else {
                SysRoleMenu roleMenu = this.getById(roleId);
                String menuId = roleMenu.getMenuId();
                if (StringUtils.isNotBlank(menuId)) {
                    Set<Integer> menuIds = Arrays.stream(menuId.split(","))
                            .map(Integer::valueOf).collect(Collectors.toSet());
                    permsSet = menuService.getBatchIds(menuIds);
                }
            }
            return permsSet;
        }
        return permsSet;
    }
}




